<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img0.baidu.com/it/u=1755781451,3332763077&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >Go结构体</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、自定义类型-amp-类型别名"><a href="#一、自定义类型-amp-类型别名" class="headerlink" title="一、自定义类型&amp;类型别名"></a>一、自定义类型&amp;类型别名</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">Go语言中没有&quot;类&quot;的概念，也不支持&quot;类&quot;的继承等面向对象的概念。Go语言中通过结构体的内嵌在配合接口比面向对象具有更高的扩展性和灵活性。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 自定义类型</span><br><span class="line">	<span class="comment">// 在Go语言中有一些基本的数据类型，如string、整型、浮点型、布尔等数据类型，Go语言中可以使用type关键字来定义自定义类型。</span></span><br><span class="line">        <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">        <span class="keyword">import</span> (</span><br><span class="line">            <span class="string">&quot;fmt&quot;</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> myInt <span class="keyword">int</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> b myInt = <span class="number">100</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%T\n&quot;</span>, b)</span><br><span class="line">        &#125;  <span class="comment">// main.myInt</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 类型别名</span><br><span class="line">    类型别名就像是我叫林汀，但是英文名是Ame，都是我本人。</span><br><span class="line">    我们之前见过的<span class="keyword">rune</span>&amp;<span class="keyword">byte</span>都是类型别名，他们的定义如下</span><br><span class="line">        <span class="keyword">type</span> <span class="keyword">byte</span> = <span class="keyword">uint8</span>	</span><br><span class="line">        <span class="keyword">type</span> <span class="keyword">rune</span> = <span class="keyword">int32</span></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 两者区别</span><br><span class="line">    <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">    <span class="keyword">import</span> (</span><br><span class="line">        <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="comment">//类型定义</span></span><br><span class="line">    <span class="keyword">type</span> NewInt <span class="keyword">int</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//类型别名</span></span><br><span class="line">    <span class="keyword">type</span> MyInt = <span class="keyword">int</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a NewInt</span><br><span class="line">        <span class="keyword">var</span> b MyInt</span><br><span class="line"></span><br><span class="line">        fmt.Printf(<span class="string">&quot;type of a:%T\n&quot;</span>, a) <span class="comment">//type of a:main.NewInt</span></span><br><span class="line">        fmt.Printf(<span class="string">&quot;type of b:%T\n&quot;</span>, b) <span class="comment">//type of b:int</span></span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">// 显示类型不一样 a是main.NewInt 类型  b是bin 这就是类型别名，可以把最本质的类型给显示出来</span></span><br></pre></td></tr></table></figure>

<h2 id="二、结构体的定义"><a href="#二、结构体的定义" class="headerlink" title="二、结构体的定义"></a>二、结构体的定义</h2><h3 id="2-1-什么是结构体"><a href="#2-1-什么是结构体" class="headerlink" title="2.1 什么是结构体"></a>2.1 什么是结构体</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Go语言中的基础数据类型可以表示一些事物的基本属性，但是当我们想表达一个事物的全部或部分属性时，这时候再用单一的基本数据类型明显就无法满足需求了，Go语言提供了一种自定义数据类型，可以封装多个基本数据类型，这种数据类型叫结构体，英文名称struct。 也就是我们可以通过struct来定义自己的类型了。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    Go语言中通过struct来实现面向对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-结构体的定义"><a href="#2-2-结构体的定义" class="headerlink" title="2.2 结构体的定义"></a>2.2 结构体的定义</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用type和struct 关键字来定义结构体</span></span><br><span class="line"><span class="number">1.</span> 定义</span><br><span class="line">    <span class="keyword">type</span> 类型名 <span class="keyword">struct</span> &#123;</span><br><span class="line">        字段名 字段类型</span><br><span class="line">        字段名 字段类型</span><br><span class="line">        ...</span><br><span class="line">    &#125;	</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">类型名: 标识自定义结构体的名称，在同一个包内不能重复</span></span><br><span class="line"><span class="comment">字段名: 表示结构体字段名。结构体中的字段名必须唯一</span></span><br><span class="line"><span class="comment">字段类型: 标识结构体字段的具体类型</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="number">2.</span> 代码演示(定义)</span><br><span class="line">    <span class="keyword">type</span> Student <span class="keyword">struct</span> &#123;</span><br><span class="line">        Name <span class="keyword">string</span></span><br><span class="line">        Age <span class="keyword">int</span></span><br><span class="line">        Gender <span class="keyword">string</span></span><br><span class="line">        Hobby []<span class="keyword">string</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> s Student </span><br><span class="line">        s.Name = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">        s.Age = <span class="number">18</span></span><br><span class="line">        s.Gender = <span class="string">&quot;male&quot;</span></span><br><span class="line">        s.Hobby = []<span class="keyword">string</span>&#123;<span class="string">&quot;喝茶&quot;</span>, <span class="string">&quot;DBJ&quot;</span>, <span class="string">&quot;五色球&quot;</span>&#125;</span><br><span class="line">        fmt.Printf(<span class="string">&quot;type of s:%T\nvalue of s: %v&quot;</span>, s, s)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-匿名结构体"><a href="#2-3-匿名结构体" class="headerlink" title="2.3 匿名结构体"></a>2.3 匿名结构体</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="comment">// 匿名结构体：适合定义一些临时数据结构等场景</span></span><br><span class="line">	<span class="keyword">var</span> s <span class="keyword">struct</span> &#123;</span><br><span class="line">		Name <span class="keyword">string</span></span><br><span class="line">		Age <span class="keyword">int</span></span><br><span class="line">	&#125;</span><br><span class="line">	s.Name = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">	s.Age = <span class="number">18</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-4-结构体指针"><a href="#2-4-结构体指针" class="headerlink" title="2.4  结构体指针"></a>2.4  结构体指针</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> Student <span class="keyword">struct</span> &#123;</span><br><span class="line">	Name <span class="keyword">string</span></span><br><span class="line">	Age <span class="keyword">int</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数内传值一切都是拷贝的意思，除非传内存地址进去</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">f</span><span class="params">(s Student)</span></span> &#123;</span><br><span class="line">	s.Name = <span class="string">&quot;Amee&quot;</span> <span class="comment">// 修改的只是函数内的副本</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">f1</span><span class="params">(s *Student)</span></span> &#123;</span><br><span class="line">	<span class="comment">// (*s).Name = &quot;Ame雨雨雨&quot; // 根据内存地址找到原变量，修改的就是原变量的值</span></span><br><span class="line">	s.Name = <span class="string">&quot;Ame雨雨雨&quot;</span> <span class="comment">// 语法糖，Go内部帮你处理了，跟上行代码一样</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> s Student</span><br><span class="line">	s.Name = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">	s.Age = <span class="number">18</span></span><br><span class="line">	f(s)</span><br><span class="line">	fmt.Println(s.Name) <span class="comment">// Ame</span></span><br><span class="line">	f1(&amp;s)</span><br><span class="line">	fmt.Println(s.Name) <span class="comment">// Ame雨雨雨</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-5-结构体初始化"><a href="#2-5-结构体初始化" class="headerlink" title="2.5 结构体初始化"></a>2.5 结构体初始化</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> 初始化方法一</span><br><span class="line">		<span class="keyword">var</span> user User</span><br><span class="line">		user.Username = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">		user.Age = <span class="number">18</span></span><br><span class="line">		user.Gender = <span class="string">&quot;male&quot;</span></span><br><span class="line">		user.Avater = <span class="string">&quot;https://www.baidu.com/xx/jpg&quot;</span>	</span><br><span class="line">		<span class="comment">// 使用变量名+ . + 字段名访问结构体中的字段</span></span><br><span class="line"></span><br><span class="line"><span class="number">2</span> 初始化方法二</span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Username <span class="keyword">string</span></span><br><span class="line">            Gender   <span class="keyword">string</span></span><br><span class="line">            Age      <span class="keyword">int</span></span><br><span class="line">            Avater   <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> user User = User&#123;</span><br><span class="line">                Username: <span class="string">&quot;Ame&quot;</span>, <span class="comment">// 使用key:value的形式初始化</span></span><br><span class="line">                Age:      <span class="number">18</span>,</span><br><span class="line">                Gender:   <span class="string">&quot;male&quot;</span>,</span><br><span class="line">                Avater:   <span class="string">&quot;https://www.baidu.com/xx.jpg&quot;</span>,</span><br><span class="line">            &#125;</span><br><span class="line">            fmt.Print(user)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">3</span> 初始化方法三</span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Username <span class="keyword">string</span></span><br><span class="line">            Gender   <span class="keyword">string</span></span><br><span class="line">            Age      <span class="keyword">int</span></span><br><span class="line">            Avater   <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            user := User&#123;</span><br><span class="line">                <span class="string">&quot;Ame&quot;</span>, <span class="comment">// 使用值得列表形式初始化，值得顺序要和结构体定义的顺序一致</span></span><br><span class="line">                <span class="string">&quot;male&quot;</span>,</span><br><span class="line">                <span class="number">18</span>,</span><br><span class="line">                Avater:   <span class="string">&quot;https://www.baidu.com/xx.jpg&quot;</span>, </span><br><span class="line">            &#125;</span><br><span class="line">            fmt.Print(user)</span><br><span class="line">        &#125;</span><br><span class="line"><span class="number">4</span> 初始化方法三</span><br><span class="line">        <span class="keyword">var</span> p = <span class="built_in">new</span>(User)</span><br><span class="line">        p2.name = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">        p2.gender = <span class="string">&quot;male&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="三、结构体的内存布局"><a href="#三、结构体的内存布局" class="headerlink" title="三、结构体的内存布局"></a>三、结构体的内存布局</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一个结构体占一块连续的内存，因为结构体的字段是连续的内存</span></span><br><span class="line"><span class="comment">// 内存对齐：不同类型占的内存是不一样的， 比如int32占4个字节， int64占8个字节</span></span><br></pre></td></tr></table></figure>

<h2 id="四、构造函数"><a href="#四、构造函数" class="headerlink" title="四、构造函数"></a>四、构造函数</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是构造函数</span><br><span class="line">	返回一个结构体变量的函数</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">    <span class="keyword">type</span> Student <span class="keyword">struct</span> &#123;</span><br><span class="line">        Name <span class="keyword">string</span></span><br><span class="line">        Age <span class="keyword">int</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 构造函数 约定成俗以new开头</span></span><br><span class="line">    <span class="comment">// 返回的是结构体还是结构体指针</span></span><br><span class="line">    <span class="comment">// 当结构体比较大的时候尽量使用结构体指针，减少程序的内存开销，最好指针</span></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">newStudent</span><span class="params">(name <span class="keyword">string</span>, age <span class="keyword">int</span>)</span> *<span class="title">Student</span></span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &amp;Student&#123;</span><br><span class="line">            Name: name,</span><br><span class="line">            Age: age,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        s1 := newstudent(<span class="string">&quot;Ame&quot;</span>, <span class="number">18</span>)</span><br><span class="line">        fmt.Println(s1)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、方法和接收者"><a href="#五、方法和接收者" class="headerlink" title="五、方法和接收者"></a>五、方法和接收者</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是方法</span><br><span class="line">	Go语言中方法是一种作用于特定类型变量的函数</span><br><span class="line">	<span class="comment">// 定义格式</span></span><br><span class="line">	<span class="function"><span class="keyword">func</span> <span class="params">(接收者变量 接收者类型)</span> 方法名<span class="params">(参数列表)</span> <span class="params">(返回参数)</span></span> &#123;</span><br><span class="line">    	函数体</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 什么是接收者</span><br><span class="line">	接受者表示的是该调用方法的具体类型变量，多用类型名首字母小写表示</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 代码演示</span><br><span class="line">        <span class="keyword">type</span> dog <span class="keyword">struct</span> &#123;</span><br><span class="line">            name <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 构造函数</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">newDog</span><span class="params">(name <span class="keyword">string</span>)</span> *<span class="title">dog</span></span> &#123;</span><br><span class="line">            <span class="keyword">return</span> &amp;dog&#123;</span><br><span class="line">                name: name,</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 方法</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(d dog)</span> <span class="title">wang</span><span class="params">()</span></span> &#123;</span><br><span class="line">            fmt.Println(<span class="string">&quot;旺旺旺&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            d1 := newDog(<span class="string">&quot;Ame&quot;</span>)</span><br><span class="line">            d1.wang()</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">// 函数不属于任何类型，但是方法属于特定的类型</span></span><br></pre></td></tr></table></figure>

<h2 id="六、值接收者-amp-指针接收者"><a href="#六、值接收者-amp-指针接收者" class="headerlink" title="六、值接收者&amp;指针接收者"></a>六、值接收者&amp;指针接收者</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 指针接收者</span><br><span class="line">	<span class="comment">// 指针类型的接收者由一个结构体的指针组成，由于指针的特性，调用方法时修改接收者指针的任意成员变量，在方法结束后，修改都是有效的</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 值接收者</span><br><span class="line">	<span class="comment">// 当方法作用于值类型接收者时，Go语言会在代码运行时将接收者的值复制一份。在值类型接收者的方法中可以获取接收者的成员值，但修改操作只是针对副本，无法修改接收者变量本身</span></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 什么时候值接用指针接收者</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    需要修改接收者中的值</span></span><br><span class="line"><span class="comment">    接收者是拷贝代价比较大的大对象</span></span><br><span class="line"><span class="comment">    保证一致性，如果有某个方法使用了指针接收者，那么其他的方法也应该使用指针接收者。</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 代码演示</span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Name <span class="keyword">string</span></span><br><span class="line">            Age <span class="keyword">int</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 构造函数</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">newUser</span><span class="params">(name <span class="keyword">string</span>, age <span class="keyword">int</span>)</span> *<span class="title">User</span></span> &#123; </span><br><span class="line">            <span class="keyword">return</span> &amp;User&#123;</span><br><span class="line">                Name: name,</span><br><span class="line">                Age: age,</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 值接收者</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(u User)</span> <span class="title">newYear0</span><span class="params">()</span></span> &#123;</span><br><span class="line">            u.Age++</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 指针接收者</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(u *User)</span> <span class="title">newYear</span> <span class="params">()</span></span> &#123;</span><br><span class="line">            u.Age++</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            user := newUser(<span class="string">&quot;Ame&quot;</span>, <span class="number">18</span>)</span><br><span class="line">            user.newYear0()</span><br><span class="line">            fmt.Println(user.Age) <span class="comment">// 18</span></span><br><span class="line">            user.newYear()</span><br><span class="line">            fmt.Println(user.Age) <span class="comment">// 19</span></span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<h2 id="七、自定义类型添加方法"><a href="#七、自定义类型添加方法" class="headerlink" title="七、自定义类型添加方法"></a>七、自定义类型添加方法</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在Go语言中，接收者的类型可以是任何类型，不仅仅是结构体，任何类型都可以拥有方法。 </span></span><br><span class="line"><span class="comment">// 注意 非本地类型不能定义方法，也就是说不能给别的包的类型定义方法</span></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 代码演示</span><br><span class="line">    <span class="comment">//MyInt 将int定义为自定义MyInt类型</span></span><br><span class="line">    <span class="keyword">type</span> MyInt <span class="keyword">int</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//SayHello 为MyInt添加一个SayHello的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="params">(m MyInt)</span> <span class="title">SayHello</span><span class="params">()</span></span> &#123;</span><br><span class="line">        fmt.Println(<span class="string">&quot;Hello, 我是一个int。&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> m1 MyInt</span><br><span class="line">        m1.SayHello() <span class="comment">//Hello, 我是一个int。</span></span><br><span class="line">        m1 = <span class="number">100</span></span><br><span class="line">        fmt.Printf(<span class="string">&quot;%#v  %T\n&quot;</span>, m1, m1) <span class="comment">//100  main.MyInt</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="八、结构体的匿名字段"><a href="#八、结构体的匿名字段" class="headerlink" title="八、结构体的匿名字段"></a>八、结构体的匿名字段</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 匿名字段</span><br><span class="line">	<span class="comment">// 就是没有名字的字段</span></span><br><span class="line">    <span class="comment">// 我们可以直接通过字段去访问</span></span><br><span class="line">            <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">                Username <span class="keyword">string</span></span><br><span class="line">                Gender   <span class="keyword">string</span></span><br><span class="line">                Age      <span class="keyword">int</span></span><br><span class="line">                <span class="keyword">int</span></span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">                <span class="keyword">var</span> user User </span><br><span class="line">                user.<span class="keyword">int</span> = <span class="number">100</span></span><br><span class="line">                fmt.Print(user.<span class="keyword">int</span>)</span><br><span class="line">            &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="九、结构体嵌套"><a href="#九、结构体嵌套" class="headerlink" title="九、结构体嵌套"></a>九、结构体嵌套</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 嵌套结构体</span><br><span class="line">        <span class="keyword">type</span> Addr <span class="keyword">struct</span>&#123;</span><br><span class="line">            address <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Username <span class="keyword">string</span></span><br><span class="line">            Gender   <span class="keyword">string</span></span><br><span class="line">            Age      <span class="keyword">int</span></span><br><span class="line">            addr Addr</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> user User </span><br><span class="line">            user.addr.address = <span class="string">&quot;NJ&quot;</span></span><br><span class="line">            fmt.Print(user.addr.address)</span><br><span class="line">    	&#125;</span><br><span class="line"><span class="number">2.</span> 匿名嵌套结构体</span><br><span class="line">        <span class="keyword">type</span> Addr <span class="keyword">struct</span>&#123;</span><br><span class="line">            address <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Username <span class="keyword">string</span></span><br><span class="line">            Gender   <span class="keyword">string</span></span><br><span class="line">            Age      <span class="keyword">int</span></span><br><span class="line">            Addr</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> user User </span><br><span class="line">            user.address = <span class="string">&quot;NJ&quot;</span></span><br><span class="line">            fmt.Print(user.address)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 匿名嵌套结构体的字段冲突</span><br><span class="line">	<span class="comment">// 其实再匿名结构体还可以直接这么寻找</span></span><br><span class="line">        <span class="keyword">type</span> Addr <span class="keyword">struct</span>&#123;</span><br><span class="line">            address <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Username <span class="keyword">string</span></span><br><span class="line">            Gender   <span class="keyword">string</span></span><br><span class="line">            Age      <span class="keyword">int</span></span><br><span class="line">            Addr</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> user User </span><br><span class="line">            user.address = <span class="string">&quot;SZ&quot;</span></span><br><span class="line">            fmt.Print(user.address)</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="comment">// 但是在这情况下出现了字段冲突怎么办</span></span><br><span class="line">        <span class="keyword">type</span> Addr <span class="keyword">struct</span>&#123;</span><br><span class="line">            address <span class="keyword">string</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">            Username <span class="keyword">string</span></span><br><span class="line">            Gender   <span class="keyword">string</span></span><br><span class="line">            address <span class="keyword">string</span></span><br><span class="line">            Age      <span class="keyword">int</span></span><br><span class="line">            Addr</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> user User </span><br><span class="line">            user.Addr.address = <span class="string">&quot;SZ&quot;</span></span><br><span class="line">            fmt.Print(user.Addr.address)</span><br><span class="line">        &#125; <span class="comment">// 直接彻底标明即可</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="十、匿名结构体模拟继承"><a href="#十、匿名结构体模拟继承" class="headerlink" title="十、匿名结构体模拟继承"></a>十、匿名结构体模拟继承</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 简单继承</span><br><span class="line">    <span class="keyword">type</span> Animol <span class="keyword">struct</span>&#123;</span><br><span class="line">        Name <span class="keyword">string</span></span><br><span class="line">        Age <span class="keyword">int</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">type</span> People <span class="keyword">struct</span> &#123;</span><br><span class="line">        Animol</span><br><span class="line">        Sex  <span class="keyword">string</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> p People</span><br><span class="line">        p.Name = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">        p.Age = <span class="number">18</span></span><br><span class="line">        p.Sex = <span class="string">&quot;male&quot;</span></span><br><span class="line">        fmt.Print(p)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 这个就是一个简单的继承，Go语言当中没有明确的面向对象，所以我们可以用匿名结构体来展示继承</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 稍微复杂一丢丢(定义了方法，需看完第八小节方法才懂)</span><br><span class="line">        <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">        <span class="keyword">import</span> (</span><br><span class="line">            <span class="string">&quot;fmt&quot;</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> Animal <span class="keyword">struct</span> &#123;</span><br><span class="line">            Name <span class="keyword">string</span></span><br><span class="line">            Age  <span class="keyword">int</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(a *Animal)</span> <span class="title">SetName</span><span class="params">(name <span class="keyword">string</span>)</span></span> &#123;</span><br><span class="line">            a.Name = name</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(a *Animal)</span> <span class="title">SetAge</span><span class="params">(age <span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            a.Age = age</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(a *Animal)</span> <span class="title">Print0</span><span class="params">()</span></span> &#123;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;a.name: %s a.age: %d\n&quot;</span>, a.Name, a.Age)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">type</span> Bird <span class="keyword">struct</span> &#123;</span><br><span class="line">            Animal</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="params">(b *Bird)</span> <span class="title">Fly</span><span class="params">()</span></span> &#123;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;name %s is fly\n&quot;</span>, b.Name)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> b *Bird = &amp;Bird&#123;&#125;</span><br><span class="line">            b.SetName(<span class="string">&quot;Ame&quot;</span>)</span><br><span class="line">            b.SetAge(<span class="number">18</span>)</span><br><span class="line">            b.Fly()</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="comment">// 继承是一种类型的继承，不是实例的继承，实例出来一个动物和一个鸟，他们都是独立的，只是鸟有动物的字段，实例化的时候还得自己赋值</span></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 多继承</span><br><span class="line">	不推荐，用了把字段得写明白</span><br></pre></td></tr></table></figure>

<h2 id="十一、结构体与JSON"><a href="#十一、结构体与JSON" class="headerlink" title="十一、结构体与JSON"></a>十一、结构体与JSON</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;encoding/json&quot;</span></span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">	Name <span class="keyword">string</span></span><br><span class="line">	Age <span class="keyword">int</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> u1 User =  User &#123;</span><br><span class="line">		Name: <span class="string">&quot;Ame&quot;</span>, <span class="comment">// 字段首字母必须大写，因为小写就是私有的，只有这个包可以调用</span></span><br><span class="line">		Age: <span class="number">18</span>,</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 序列化</span></span><br><span class="line">	b, err := json.Marshal(u1)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		fmt.Println(err)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	fmt.Printf(<span class="string">&quot;%v\n&quot;</span>, <span class="keyword">string</span>(b)) <span class="comment">// 因为是byte类型，所以可以用string强转</span></span><br><span class="line">	<span class="comment">// 反序列化</span></span><br><span class="line">	str := <span class="string">`&#123;&quot;name&quot;: &quot;Ame&quot;, &quot;age&quot;: 18&#125;`</span></span><br><span class="line">	<span class="keyword">var</span> u2 User</span><br><span class="line">	json.Unmarshal([]<span class="keyword">byte</span>(str), &amp;u2) <span class="comment">// 要传入内存地址，要不然只会修改副本</span></span><br><span class="line">	fmt. Printf(<span class="string">&quot;%#v&quot;</span>, u2)</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">   </span><br><span class="line">  tag是结构体的元信息，可以在运行的时候通过反射的机制读取出来</span><br><span class="line"><span class="comment">// 字段类型后面，以反引号括起来的key-value结构的字符串，多个tag以逗号隔开。</span></span><br><span class="line">       <span class="keyword">package</span> mainimport (</span><br><span class="line">       <span class="string">&quot;encoding/json&quot;</span></span><br><span class="line">       <span class="string">&quot;fmt&quot;</span></span><br><span class="line">   )</span><br><span class="line"></span><br><span class="line">   <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">       Username <span class="keyword">string</span> <span class="string">`json:&quot;username&quot;`</span> <span class="comment">// 冒号后面千万不能加空格</span></span><br><span class="line">       Sex      <span class="keyword">string</span> <span class="string">`json:&quot;sex&quot;`</span> <span class="comment">// 冒号后面千万不能加空格</span></span><br><span class="line">       Age      <span class="keyword">int</span>    <span class="string">`json:&quot;age&quot;`</span> <span class="comment">// 冒号后面千万不能加空格</span></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">       <span class="keyword">var</span> user User</span><br><span class="line">       user.Username = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">       user.Age = <span class="number">18</span></span><br><span class="line">       user.Sex = <span class="string">&quot;male&quot;</span></span><br><span class="line">       data, err := json.Marshal(user)</span><br><span class="line">       <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">           fmt.Print(err)</span><br><span class="line">           <span class="keyword">return</span></span><br><span class="line">       &#125;</span><br><span class="line">       fmt.Println(<span class="keyword">string</span>(data))</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/16/Go%E7%BB%93%E6%9E%84%E4%BD%93------%E7%BB%93%E6%9E%84%E4%BD%93/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/16/Go%E7%BB%93%E6%9E%84%E4%BD%93------%E7%BB%93%E6%9E%84%E4%BD%93/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
